//
//  OADepAddressController.m
//  OASystem
//
//  Created by zhoupengfei on 16/1/17.
//  Copyright © 2016年 zpf. All rights reserved.
//
#import "nameTableViewCell.h"
#import "PasswordView.h"

#import "OADepAddressController.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "OAAddressDetailViewController.h"
#import "Toast+UIView.h"
@interface DepAddressCellList : UITableViewCell
//@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *subject;
@property (weak, nonatomic) IBOutlet UILabel *des;


@end

@implementation DepAddressCellList



@end

@interface OADepAddressController (){
    UIButton *btn;
    PasswordView *passView;
}

@end

@implementation OADepAddressController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    return _contents;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.contents.count == 0) {
          [self requestData];
    }else{
            [self.tableView reloadData];

    }
    [self setRightItem];
}

-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
//    self.navigationItem.rightBarButtonItem = rightitem;
}


-(void)searchAction{
    
   _alertView1 = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    _alertView1.delegate = self;
    _alertView1.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [_alertView1 textFieldAtIndex:0];
    textField.placeholder = @"请输入意见";
    [_alertView1 show];
}
//搜素通讯录
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView ==_alertView1){
    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"condition":textField.text},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:@"adressListSearch" success:^(id JSON) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            NSArray * tempArray = JSON[@"data"];
            NSDictionary *dict = [tempArray firstObject];
            
            if (dict) {
//                NSArray * array = @[dict];
                self.contents = [tempArray mutableCopy];
            }
            if (self.contents.count == 0) {
                [self.view makeToast:@"搜索无相关数据"];
            }
            
            [self.tableView reloadData];
            
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    }
    }else{
    NSLog(@"钉钉");
    }
}


-(void)requestData{
      [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
   // NSArray *  array =[NSArray arrayWithObjects:@{@"path":self.path}, nil];
        

      NSArray *  array = @[@{@"unid":self.unid},@{@"path":self.path}];
    
    
    
      [HttpTools requestWithURL:KExchother arrays:array method:@"depTxlTree" success:^(id JSON) {
      NSLog(@"%@",JSON);
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
          if (self.contents.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
     NSDictionary *dict = self.contents[indexPath.row];
//       if ([dict[@"phone"]isEqualToString:@""]) {
//    cell.phone = @"no";
//}
//else{
 static NSString * cellIdentifier = @"cellId";
// nameTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    nameTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (cell == nil) {
                cell = [[nameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                                                           }
cell.button3.tag = indexPath.row;
[cell.button3 addTarget:self action:@selector(dingdingaction:) forControlEvents:UIControlEventTouchUpInside];

         if ([dict[@"phone"]isEqualToString:@""]) {
    cell.phone = @"no";
}if ([dict[@"phone1"]isEqualToString:@""]) {
    cell.phone1 = @"no";
}

    cell.dict = dict;
//}
    return cell;
}
- (void)dingdingaction:(UIButton *)btn{
NSDictionary *dic = self.contents[btn.tag];
//_alertView2 = [[UIAlertView alloc] initWithTitle:@"钉钉消息" message:NSLocalizedString(@"\n\n\n\n\n\n", nil) delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
////    _alertView2.delegate = self;
////    _alertView2.alertViewStyle = UIAlertViewStylePlainTextInput;
////    UITextField * textField = [_alertView2 textFieldAtIndex:0];
////    textField.placeholder = @"请输入意见";
//
//  UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(10, 10, 80, 40)];
//    [_alertView2 addSubview:textView];
//
//    [_alertView2 show];
 passView = [[PasswordView alloc] initWithTitle:@"钉钉消息" cancelBtn:@"取消" sureBtn:@"发送" btnClickBlock:^(NSInteger index,NSString *str) {
        if (index == 0) {
            NSLog(@"0000000");
        }else if (index == 1){
            NSLog(@"111111");
            NSLog(@"^^^^^%@",str);
           str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 if ([str isEqualToString:@""]|| str.length == 0)
{
     [self.view makeToast:NSLocalizedString(@"钉钉消息为空", nil)];
} else{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray *  array = @[@{@"userId":userId},@{@"con":str},@{@"rec":dic[@"phone"]}];;
[HttpTools requestWithURL:@"exchsendinfo" arrays:array method:@"senddinginfo" success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSLog(@"%@",JSON);
       if ([[NSString stringWithFormat:@"%@",JSON]isEqualToString:@"25"]) {
         [self.view makeToast:NSLocalizedString(@"发送成功", nil)];

}else{

         [self.view makeToast:NSLocalizedString(@"发送失败", nil)];

}
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
}

            
            
            
        }
    }];
    [passView show];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 return 44;
 }

-(UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OAAddressDetailViewController * detailVC = (OAAddressDetailViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"AddressDetailId"];
     NSDictionary *dict = self.contents[indexPath.row];
    detailVC.unid = dict[@"unid"];
                             detailVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
